package com.unisound.tracelog.server.ui.converter;

import com.unisound.tracelog.server.model.entity.TraceLog;
import com.unisound.tracelog.server.model.entity.TraceLogTag;
import com.unisound.tracelog.server.service.TraceLogService;
import com.unisound.tracelog.server.ui.model.response.TraceLogTagResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class TraceLogTagToResponseConverter implements Converter<TraceLogTag, TraceLogTagResponse> {

    @Autowired
    private TraceLogService traceLogService;

    @Override
    public TraceLogTagResponse convert(TraceLogTag source) {
        TraceLog traceLog = traceLogService.getById(source.getTraceId());
        return TraceLogTagResponse.builder()
                .traceId(source.getTraceId())
                .tagKey(source.getTagKey())
                .tagValue(source.getTagValue())
                .traceStartTime(traceLog == null ? null : traceLog.getStartTime())
                .traceEndTime(traceLog == null ? null : traceLog.getEndTime())
                .build();
    }

}
